<?php require_once('../connections/con_gl.php'); ?>
<?php
$act = $_GET[act];
$url = "index.php?component=kontrak";
$ope = $_POST['open'];
// -- validasi
if($act <>'delete') { 
	if (trim($_POST['noreferensi']) == '') {
		$error[] = '- No Kontrak harus diisi !!!';
	}
	// cek kontrak
	mysql_select_db($database_con_gl, $con_gl);
	$query_cekpk = "select no from gl_kontrak where no='$_POST[noreferensi]'";
	$cekpk       = mysql_query($query_cekpk, $con_gl) or die(mysql_error());
	$total_cekpk = mysql_num_rows($cekpk);
	if($total_cekpk > 0 AND $act=='add') {
		$error[] = '- No. Kontak <b>'.$_POST[noreferensi].'</b> sudah digunakan !!!';
	}
	//----
	if ((trim($_POST['tanggal']) == '')) {
		$error[] = '- Tanggal harus diisi !!!';
	}
	if (trim($_POST['dari']) == '') {
		$error[] = '- Customer harus dipilih !!!';
	}
	if ((trim($_POST['awal']) == '') OR (trim($_POST['akhir']) == '')) {
		$error[] = '- Periode Kontrak harus diisi !!!';
	}
	if (trim($_POST['durasi']) == '') {
		$error[] = '- Durasi Kontrak harus dipilih !!!';
	}
	if (trim($_POST['pembayaran']) == '') {
		$error[] = '- Periode Pembayaran harus dipilih !!!';
	}
	if ($_POST['ntotal'] <= 0) {
		$error[] = '- Periksa Total Transaksi anda !!!';
	}
}
/*End validasi */
if (isset($error)) {
	echo "<img src=\"images/alert.png\" width=\"16\" align=\"left\"/>&nbsp;&nbsp;<b style=\"color:red;\">Error : </b> <br />".implode("<br />", $error);
}else{
	switch($act) {
	case("delete"):
		$query = "delete from gl_kontrak where no='$_POST[noreferensi]'";
		$query2 = "delete from gl_dkontrak where kontrak='$_POST[noreferensi]'";
		// hapus data
		mysql_select_db($database_con_gl, $con_gl);
		$runquery = mysql_query($query, $con_gl);
		if($runquery) { 
			// ---
			mysql_query($query2, $con_gl);
			// ---
			echo "<img src=\"images/ok.png\" align=\"left\" width=\"16\">&nbsp;&nbsp;Data berhasil dihapus ...";
			echo "<script type=\"text/javascript\">setTimeout(\"location.href='".$url."'\", 2000);</script>";
		}else{
			echo "<img src=\"images/alert.png\" align=\"left\" width=\"16\"> Data gagal dihapus !!!";
		}
	break;
	case("edit"):
		$query="update gl_kontrak set nopo='$_POST[nopo]', kontak='$_POST[dari]', recipient='$_POST[recipient]', durasi='$_POST[durasi]', pbayar='$_POST[pembayaran]', gudang='$_POST[gudang]', tgl='$_POST[tanggal]', mulai='$_POST[awal]', sampai='$_POST[akhir]', info='$_POST[keterangan]', biaya='$_POST[biaya]', pajak='$_POST[pajak]', total='$_POST[ntotal]' where no='$_POST[noreferensi]'";
		//-- save data
		mysql_select_db($database_con_gl, $con_gl);
		$runquery = mysql_query($query, $con_gl);
		if($runquery) { 
			echo "<img src=\"images/ok.png\" align=\"left\" width=\"16\">&nbsp;&nbsp;Data berhasil disimpan ...";
			echo "<script type=\"text/javascript\">setTimeout(\"location.href='".$url."'\", 2000);</script>";
		}else{
			echo "<img src=\"images/alert.png\" align=\"left\" width=\"16\"> Data gagal disimpan !!!";
		}
	break;
	default:
		// ---
		$query = "insert into gl_kontrak values ('$_POST[noreferensi]','$_POST[nopo]','$_POST[dari]','$_POST[recipient]','$_POST[durasi]','$_POST[pembayaran]','$_POST[gudang]','$_POST[tanggal]','$_POST[awal]','$_POST[akhir]','$_POST[keterangan]','$_POST[biaya]','$_POST[pajak]','$_POST[ntotal]')";
		// save data
		mysql_select_db($database_con_gl, $con_gl);
		$runquery = mysql_query($query, $con_gl);
		if($runquery) { 
			echo "<img src=\"images/ok.png\" align=\"left\" width=\"16\">&nbsp;&nbsp;Data berhasil disimpan ...";
			echo "<script type=\"text/javascript\">setTimeout(\"location.href='".$url."'\", 2000);</script>";
		}else{
			echo "<img src=\"images/alert.png\" align=\"left\" width=\"16\"> Data gagal disimpan !!!";
		}
	break;
	}
}
?>